//app.js
App({
  onLaunch: function () {
    var that = this;
    wx.checkSession({
        success: function () {
          //session_key 未过期，并且在本生命周期一直有效
        },
        fail: function () {
          // session_key 已经失效，需要重新执行登录流程
          wx.login({
            success: res => {
              // 发送 res.code 到后台换取 openId, sessionKey, unionId
              var code = res.code;
              that.token(code);
            }
          }) //重新登录
        }
    })
    // 登录
    wx.login({
      success: res => {
        //console.log(res);
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var code = res.code;
        that.token(code);
      }
    })
  },
  login(userinfo, callback) {
    var that=this;
    if (userinfo.detail.errMsg == 'getUserInfo:ok') {
      var userinfo1 = userinfo.detail.userInfo;
      wx.request({
        url: that.globalData.directory + 'api/v2/user/getinfo',
        method: 'POST',
        data: {
          data: userinfo1
        },

        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token')
        },
        success: function (res) {
          that.denglu();
          callback(false, null)
        },
      })
    }
    else if (userinfo.detail.errMsg == 'getUserInfo:fail auth deny') { // 当用户点击拒绝时
      wx.showModal({
        title:'不授权无法正常使用功能'
      }) // 提示用户，需要授权才能登录
      callback('fail to modify scope', null)
    }
  },
  /*判断积分优惠券是否能够使用 */
  couponpoints:function(){
    var that=this;
    wx.request({
      url: that.globalData.directory + 'api/v2/pay/facepay',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.errMsg == 'request:ok') {
          that.globalData.coupon = res.data.coupon;
          that.globalData.points = res.data.points;
        } else {
          wx.showToast({
            title: '请求失败,请重试',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },
  denglu: function (userinfo1) {
    var that = this;
    wx.request({
      url: that.globalData.directory + 'api/v2/user/getuser',
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if(res.data.status=='ok'){
         // console.log(res);
          that.globalData.user = res.data.data;
        }
      },
    })
  },
  shopname:function(){
    var that=this;
    wx.request({
      url: that.globalData.directory + 'api/v2/getshopinfo',
      header: {
        'content-type': 'application/json', // 默认值
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          that.globalData.shopname=res.data.data;
        } else {
          wx.showToast({
            title: '请求失败,请稍候',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    });
  },
  token: function (code) {
    var that = this;
    wx.request({
      url: that.globalData.directory + 'api/v2/token/user',
      method: 'POST',
      data: {
        code: code
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var token = res.data.data;
        if (res.data.status == 'ok') {
          wx.request({
            url: that.globalData.directory + 'api/v2/token/verify',
            method: 'POST',
            data: {
              token: token
            },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              if (res.data.status == 'ok') {
                wx.setStorageSync('token', token);
                that.denglu();
                that.couponpoints();
                that.shopname();
                that.foodall();
              } else {
                if (that.globalData.demo > 5) {
                  return false;
                }
                that.globalData.demo++;
                that.token();
                wx.showToast({
                  title: '请求失败,请重试',
                  icon: 'none',
                  duration: 2000,
                })
              }
            }
          })
        } else {
          wx.showToast({
            title: '请求失败,请重试',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },
  foodall:function(){
    var that=this;
    wx.request({
      url: that.globalData.directory + 'api/v2/getGoodsAll',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          that.globalData.foodall=res.data.data
        } else {
          wx.showToast({
            title: '请求失败,请重试',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },
  globalData: {
    userInfo: null,
    // directory: 'https://p.wechat.lbdgkj.com/',
     directory: 'http://192.168.0.74/',
    // directory: '192.168.0.74',
    // directory: 'http://lssfood.com/',
    user: {},//用户
    choosewelfare:'',//选择的优惠券
    coupon:{},//优惠券
    points:{},//积分
    tableid:'231',//桌号
    shopname:{},//店家
    foodall:{},//食物
    demo: 0,
  }
})